<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Input Output Helper
 * 
 * deepsteal@hotmail.com
 */
// ------------------------------------------------------------------------

/**
 * xml_array_for_render
 * 
 * Retorna array de un array o string dado
 * 
 * @author Edgardo Segura 
 * @example
 * @see
 * @link {}
 * @since 16/02/2009
 * @version
 * @access	public
 * @param	array
 * @return	array
 */	
if ( ! function_exists('xml_array_for_render'))
{
	function xml_array_for_render($xml_array){
		$array_cero = array_keys($xml_array);
		$array_cero = $array_cero[0];
		$array_uno = array_keys($xml_array[$array_cero]);
		$array_uno = $array_uno[0];	
	return $xml_array[$array_cero][$array_uno];
	}
}
// ------------------------------------------------------------------------

/**
 * Json_decode
 *
 * @author Edgardo Segura 
 * @example
 * @see
 * @link {}
 * @since 16/02/2009
 * @version
 * @access	public
 * @param	string
 * @return	string
 */
if ( !function_exists('json_decode') )
{
	function json_decode($content, $assoc=false)
	{
		require_once 'system/application/libraries/Addons/Json.php';
        if ($assoc)
        {
			$json = new Services_JSON(SERVICES_JSON_LOOSE_TYPE);
        } 
        else
		{
			$json = new Services_JSON;
		}
		return $json->decode($content);
    }
}
// ------------------------------------------------------------------------

/**
 * Json_encode
 *
 * Retorna array con nodos de un XML
 * 
 * @author Edgardo Segura 
 * @example
 * @see
 * @link {}
 * @since 16/02/2009
 * @version
 * @access	public
 * @param	string
 * @return	string
 */
if ( !function_exists('json_encode') )
{
	function json_encode($content)
	{
		require_once 'system/application/libraries/Addons/Json.php';
		$json = new Services_JSON;
               
		return $json->encode($content);
    }
}
// ------------------------------------------------------------------------

/**
 * Array2xml
 *
 * Convierte array en XML
 * 
 * @author Edgardo Segura 
 * @example
 * @see
 * @link {}
 * @since 16/02/2009
 * @version
 * @access	public
 * @param	array
 * @return	string
 */
if ( !function_exists('array2xml') )
{
	function array2xml($content)
	{
		require_once 'system/application/libraries/Addons/Xml/Xml.php';
		$xml = new xml();

		// Set the array so the class knows what to create the XML from
		$xml->setArray($content);

		// Return the XML
		return $xml->outputXML();
    }
}
// ------------------------------------------------------------------------

/**
 * XMLtoArray
 *
 * Retorna array con nodos de un XML (utilizar file_get_contents o similar para formar string del archivo xml)
 *
 * @author Edgardo Segura 
 * @example
 * @see
 * @link {}
 * @since 16/02/2009
 * @version
 * @access	public
 * @param	string
 * @return	array
 */	
if ( ! function_exists('XMLtoArray'))
{
	function XMLtoArray($XML)
	{
	    $xml_parser = xml_parser_create();
	    xml_parse_into_struct($xml_parser, $XML, $vals);
	    xml_parser_free($xml_parser);
	    $_tmp='';
	    foreach ($vals as $xml_elem)
	    {
	        $x_tag=$xml_elem['tag'];
	        $x_level=$xml_elem['level'];
	        $x_type=$xml_elem['type'];
	        if ($x_level!=1 && $x_type == 'close')
	        {
	            if (isset($multi_key[$x_tag][$x_level]))
	                $multi_key[$x_tag][$x_level]=1;
	            else
	                $multi_key[$x_tag][$x_level]=0;
	        }
	        if ($x_level!=1 && $x_type == 'complete')
	        {
	            if ($_tmp==$x_tag)
	                $multi_key[$x_tag][$x_level]=1;
	            $_tmp=$x_tag;
	        }
	    }
	    foreach ($vals as $xml_elem)
	    {
	        $x_tag=$xml_elem['tag'];
	        $x_level=$xml_elem['level'];
	        $x_type=$xml_elem['type'];
	        if ($x_type == 'open')
	            $level[$x_level] = $x_tag;
	        $start_level = 1;
	        $php_stmt = '$xml_array';
	        if ($x_type=='close' && $x_level!=1)
					{
	            $multi_key[$x_tag][$x_level]++;
					}
	        while($start_level < $x_level)
	        {
	              $php_stmt .= '[$level['.$start_level.']]';
	              if (isset($multi_key[$level[$start_level]][$start_level]) && $multi_key[$level[$start_level]][$start_level])
	                  $php_stmt .= '['.($multi_key[$level[$start_level]][$start_level]-1).']';
	              $start_level++;
	        }
	        $add='';
	        if (isset($multi_key[$x_tag][$x_level]) && $multi_key[$x_tag][$x_level] && ($x_type=='open' || $x_type=='complete'))
	        {
	            if (!isset($multi_key2[$x_tag][$x_level]))
	                //$multi_key2[$x_tag][$x_level]=0;
									$multi_key2[$x_tag][1]=0;
	            else
	             		$multi_key2[$x_tag][$x_level]++;
	            $add='['.$multi_key2[$x_tag][$x_level].']';
	        }
	        if (isset($xml_elem['value']) && trim($xml_elem['value'])!='' && !array_key_exists('attributes',$xml_elem))
	        {
	            if ($x_type == 'open')
	                $php_stmt_main=$php_stmt.'[$x_type]'.$add.'[\'content\'] = $xml_elem[\'value\'];';
	            else
	                $php_stmt_main=$php_stmt.'[$x_tag]'.$add.' = $xml_elem[\'value\'];';
	            eval($php_stmt_main);
	        }
	        if (array_key_exists('attributes',$xml_elem))
	        {
	            if (isset($xml_elem['value']))
	            {
	                $php_stmt_main=$php_stmt.'[$x_tag]'.$add.'[\'content\'] = $xml_elem[\'value\'];';
	                eval($php_stmt_main);
	            }
	            foreach ($xml_elem['attributes'] as $key=>$value)
	            {
	                $php_stmt_att=$php_stmt.'[$x_tag]'.$add.'[$key] = $value;';
	                eval($php_stmt_att);
	            }
	        }
	    }
	     return $xml_array;
	}
}
// ------------------------------------------------------------------------

/**
 * Output_format
 *
 * Convierte array a diferentes formatos
 *
 * @author Edgardo Segura 
 * @example
 * @see
 * @link {}
 * @since 16/02/2009
 * @version
 * @access	public
 * @param	array
 * @return	multiple
 */	
if ( ! function_exists('output_format'))
{
	function output_format($format,$content,$return=TRUE){
		
		if(!is_array($content))
		{
			msg_alert('El contenido pasado a la funcion OUTPUT_FORMAT no es un array');						
		}
		else
		{
			switch ($format)
			{
				case 'json':				
					$result[]= json_encode($content);
				break;
				
				case 'xml':
					$result[]= array2xml($content);
				break;
				
				default:
					$result[]= '<pre>';
					$result[]= print_r($content,TRUE);
					$result[]= '</pre>';							
				break;
			}
			
			if($return==TRUE)
			{
				return implode('',$result);
			}
			else
			{
				echo implode('',$result);
			}
				
		}		
	}
}

/**
 * Msg_alert
 *
 * Imprime un mensaje de alerta
 * 
 * @author Edgardo Segura 
 * @example
 * @see
 * @link {}
 * @since 16/02/2009
 * @version
 * @access	public
 * @param	string
 * @return	string
 */
if ( ! function_exists('msg_alert'))
{
	function msg_alert($string,$return=FALSE,$exit=FALSE){
			$result[]= '<span style="padding:2px; border:1px solid #CC0000; margin:2px; background-color:#FFFFFF; font-size:12px; color:#CC0000">';
			$result[]= ucfirst($string);
			$result[]= '</span>';
			
			if($return==FALSE)
			{
				echo implode('',$result);
			}
			else
			{
				return implode('',$result);
			}
			
			if($exit==TRUE)
			{
				exit(1);
			}
	}
}
// ------------------------------------------------------------------------

/* End of file io_my_helper.php */
/* Location: ./system/application/helpers/io_my_helper.php */